package com.share.user.service.impl;

import java.util.List;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.common.core.context.SecurityContextHolder;
import com.share.user.domain.UpdateUserLogin;
import com.share.user.domain.UserLoginLog;
import com.share.user.mapper.UserLoginLogMapper;
import me.chanjar.weixin.common.error.WxErrorException;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.share.user.mapper.UserInfoMapper;
import com.share.user.domain.UserInfo;
import com.share.user.service.IUserInfoService;

/**
 * 用户Service业务层处理
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private UserLoginLogMapper userLoginLogMapper;
    @Autowired
    private WxMaService wxMaService;
    /**
     * 查询用户列表
     *
     * @param userInfo 用户
     * @return 用户
     */
    @Override
    public List<UserInfo> selectUserInfoList(UserInfo userInfo) {
        return userInfoMapper.selectUserInfoList(userInfo);
    }

    /**
     * 处理微信用户登录请求
     */
    @Override
    public UserInfo wxLogin(String code) {
        // 1.通过code+appid+秘钥向微信第三方api发起调用，获得openId
        String openid;
        try {
            WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(code);
            openid = sessionInfo.getOpenid();

            // 2.通过openid查db 如果无数据说明第一次登录，将该用户数据插入db
            LambdaQueryWrapper<UserInfo> lambdaQueryWrapper = new LambdaQueryWrapper<UserInfo>()
                    .eq(UserInfo::getWxOpenId,openid);
            UserInfo userInfo = userInfoMapper.selectOne(lambdaQueryWrapper);

            //如果没查询到数据，说明是第一次登录
            if (userInfo == null){
                userInfo = new UserInfo();
                userInfo.setNickname(String.valueOf(System.currentTimeMillis()));
                userInfo.setAvatarUrl("");
                userInfo.setWxOpenId(openid);

                userInfoMapper.insert(userInfo);
            }

            // 3.返回UserInfo
            return userInfo;

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Boolean updateUserLogin(UpdateUserLogin updateUserLogin) {
        //更新用户登录信息 ip 最后一次登录时间
        UserInfo userInfo = new UserInfo();
        userInfo.setId(updateUserLogin.getUserId());
        userInfo.setLastLoginIp(updateUserLogin.getLastLoginIp());
        userInfo.setLastLoginTime(updateUserLogin.getLastLoginTime());
        userInfoMapper.updateById(userInfo);

        //记录登录日志
        UserLoginLog userLoginLog = new UserLoginLog();
        userLoginLog.setUserId(userInfo.getId());
        userLoginLog.setMsg("小程序登录");
        userLoginLog.setIpaddr(updateUserLogin.getLastLoginIp());
        userLoginLogMapper.insert(userLoginLog);

        return true;
    }

    //判断用户是否免押金 模拟调用微信api  统一免押金
    @Override
    public Boolean isFreeDeposit() {
        //微信支付分
        //https://pay.weixin.qq.com/wiki/doc/apiv3/payscore.php?chapter=18_1&index=2

        // 默认免押金，模拟实现统一免押金
        UserInfo userInfo = userInfoMapper.selectById(SecurityContextHolder.getUserId());
        userInfo.setDepositStatus("1");
        userInfoMapper.updateById(userInfo);

        return true;
    }
}
